package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    //公寓信息
   @Autowired
   private ApartmentInfoMapper apartmentInfoMapper;
   @Autowired
   private ApartmentInfoService apartmentInfoService;
    //图片信息系
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    //属性信息
    @Autowired
    private AttrValueMapper attrValueMapper;
    //配套信息
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    //标签信息
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    //支付方式信息
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    //杂费信息
    @Autowired
    private FeeValueMapper feeValueMapper;

    //租期信息
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    //是否删除  is_delete
    //是否入住  is_checkin

    //浏览历史
    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Override
    public void selectListPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customSelectPageList(page, queryVo);
    }

    @Override
    public RoomDetailVo roomDetailVoById(Long id,Long userId) {

        RoomInfo roomInfo = roomInfoMapper.selectRoomById(id);
        System.out.println("id = " + id);
        System.out.println("roomInfo = " + roomInfo);
        if (roomInfo == null){
            throw new LeaseException(ResultCodeEnum.ILLEGAL_REQUEST);
        }

        //公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentItemVoById(roomInfo.getApartmentId());
        //图片信息系
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphInfoByItemTypeAndId(ItemType.ROOM,id);
        //属性信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);
        //配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
        //标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
        //支付方式信息
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
        //杂费信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByRoomId(id);
        //租期信息
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
        //是否入住
        LambdaQueryWrapper<LeaseAgreement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LeaseAgreement::getRoomId, roomInfo.getId());
        queryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        Long singedCount = leaseAgreementMapper.selectCount(queryWrapper);

        RoomDetailVo appRoomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, appRoomDetailVo);
        //是否删除
        appRoomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        appRoomDetailVo.setIsCheckIn(singedCount > 0);

        appRoomDetailVo.setApartmentItemVo(apartmentItemVo);
        appRoomDetailVo.setGraphVoList(graphVoList);
        appRoomDetailVo.setAttrValueVoList(attrValueVoList);
        appRoomDetailVo.setFacilityInfoList(facilityInfoList);
        appRoomDetailVo.setLabelInfoList(labelInfoList);
        appRoomDetailVo.setPaymentTypeList(paymentTypeList);
        appRoomDetailVo.setFeeValueVoList(feeValueVoList);
        appRoomDetailVo.setLeaseTermList(leaseTermList);

        browsingHistoryService.saveHistory(userId,id);
        return appRoomDetailVo;



    }

    @Override
    public void selectRoomInfoByApartmentIdForPage(IPage<RoomItemVo> page, Long id) {
         roomInfoMapper.selectRoomInfoByApartmentIdForPage(page,id);
    }

}




